<?php
class class_import
{
	//Khai bao bien

	function class_import()	
	{
		//global ;
		//Khoi tao bien
		
	}
	//thay the gia tri nhu submit file, button...
	function loadtemplate($arr)
	{
		$html = "";
		$html = loadHtml($arr["layout_html"],"");
		if (count($arr) > 0){
			foreach ($arr as $key=>$value){
				//$$key = $value;	//gan gia tri vao bien
				$pattern = "@$key@";
				$replacement = "$value";
				$html = ereg_replace($pattern, $replacement, $html);
			}//end for
		}
		return $html;
	}

	function _list($arr, $arr_column){
		global $which_page, $page_size_admin, $page_block, $dbconn, $txtTimkiem, $cat_parent_filter, $cat_parent_filter, $cat_parent_filter_2;
		
		$page_size = $page_size_admin;

		//load var
		$html = $this->loadtemplate($arr);
		//load templates
		$main_list = loadHtml($arr["tmp_list"],"");		
		$html = ereg_replace("@main_list@", $main_list, $html);
		
		//===============
		// Lay du lieu
		
		if($cat_parent_filter != "" && $cat_parent_filter != "0"){
			$str_where .= " and state='$cat_parent_filter' ";
		}
		if($cat_parent_filter_2 != "" && $cat_parent_filter_2 != "0"){
			$str_where .= " and city='$cat_parent_filter_2' ";
		}		
		
		if($txtTimkiem != ""){
			$str_where .= " and name like '%$txtTimkiem%' ";
		}		
		
		
		$strSql = "select * from import where 1=1 ";
		$strSql .= $str_where;
		$strSql .= " order by name";
		
		//echo "$strSql<br>";

		$rs_if= new db_query($dbconn,$strSql);
		if ($rs_if->getrow()){
			$totalrows = mysql_affected_rows();
			//Get Paging
			if(!$which_page || !is_numeric($which_page)){
				$which_page = 1;
			}
			$num_page = (int)($totalrows/$page_size);
			if($totalrows%$page_size != 0){
				$num_page ++;
			}
			//$num_row = $page_size;
			if($which_page > $num_page) $which_page = $num_page;
			if($which_page == 1){
				$index = 0;
			}else{
				$index = ($which_page*$page_size) - $page_size;
			}
		
			$body = "";
			$tmp_content = loadHtml($arr["tmp_list_content"], "");
			$i=0;
			$rs= new db_query($dbconn,$strSql);
			while ($rs->getrow())
			{ //begin While
				if (($i >= $index) && ($i < $page_size + $index))
				{
					$stt = $i+1;
					$body .= $tmp_content;
					$id = $rs->row["id"];
					$body = ereg_replace("@id@","$id",$body);
					
					
					foreach ($arr_column as $value){
						$value_pattern = "@$value@";
						
						switch ($value) {
							case "img1":
								$img1 = $rs->row["$value"];
								if($img1 != ""){
									$img_fullpath = $arr["upload_path"].$img1;
									//echo "img_fullpath: $img_fullpath<br>";
									if (file_exists($img_fullpath)){
										//echo "ton tai <br>";
										$popsize=getimagesize($img_fullpath) ;
										$img_width = htmlspecialchars($popsize[0]);
										$img_height = htmlspecialchars($popsize[1]);
										//echo "img_width: $img_width - img_height: $img_height - max=".SP_IMAGES_SMALL_WIDTH_ADMIN."<br>";
										if ($img_width > SP_IMAGES_SMALL_WIDTH_ADMIN){
											$img_tile = ceil((SP_IMAGES_SMALL_WIDTH_ADMIN*100)/$img_width);
											$img_width = SP_IMAGES_SMALL_WIDTH_ADMIN;
											$img_height = ceil(($img_tile*$img_height)/100);
											//echo "img_tile: $img_tile - img_width: $img_width - img_height: $img_height<br>";
										}																
										$value_replacement = "<img src='".$img_fullpath."' width='$img_width' height='$img_height' border=0>";
										$value_replacement = "<a href='import.php?which_page=@which_page@&act=edit&id=$id' class='listitem'>$value_replacement</a>";
				
									}else{
										$value_replacement = "&nbsp";	
									}
								}else{
									$value_replacement = "&nbsp";
								}
								break;
							case "tel":
								$tel1 = $rs->row['tel1'];
								$tel2 = $rs->row['tel2'];
								$tel3 = $rs->row['tel3'];
								$value_replacement = "($tel1) $tel2-$tel3";
								break;
							default:
								$value_replacement = $rs->row["$value"];
						}//end switch
		
						$body = ereg_replace("$value_pattern", "$value_replacement",$body);
					}//end for
				}
				 $i++;
			}//end While
			$StringQuery = "\"";
			$str_paging = "";
			if($num_page>1){
				if($which_page > 1 ){
					$str_paging = "<a href=\"?which_page=1$StringQuery> [First] </a>";
					$str_paging .= "&nbsp;<a href=\"?which_page=".($which_page-1)."$StringQuery  >[Previous]</a>&nbsp;";
				}else{
					$str_paging = "";
				}
			
			//=======================================================
				//Duc Manh them vao		
				$str_paging .= list_page($which_page,$num_page,$page_block,$StringQuery);
			//========================================================
			
				if($which_page < $num_page ){
					$str_paging .= "&nbsp;<a href=\"?which_page=".($which_page+1)."$StringQuery  > [Next]  </a>";
					$str_paging .= "<a href=\"?which_page=".($num_page)."$StringQuery> [Last]  </a>";
				}else{
					$str_paging .= "";
				}
			}//end if $num_page > 1
		}//end if rs_if

		//===============
		
		$record_num= "T&#7893;ng c&#7897;ng: ".$i."&nbsp;n&#7897;i dung ";
		$record_num .= " - Trang $which_page/$num_page ";

		$html = ereg_replace("<!--list_content-->", $body, $html);
		$html = ereg_replace("@PAGE@", "$str_paging", $html);
		$html = ereg_replace("@TOTAL@", "$record_num", $html);
		$html = ereg_replace("@which_page@", "$which_page", $html);				
		$StringQuery = ereg_replace("\"","",$StringQuery);
		$html = ereg_replace("@StringQuery@", "$StringQuery", $html);
		
		return $html;
	}
	
	function _new($arr, $arr_column){
		global $which_page, $page_size, $page_block, $dbconn;

		$html = $this->loadtemplate($arr);

		//Hien thi Field
		$body = "";
		$tmp_content = loadHtml($arr["tmp_list_content"], "");
		if (count($arr_column) > 0){
			foreach ($arr_column as $key=>$arr_value){
				//print "$key<br>";
				//print_r ($arr_value);
				//echo "<br><br>";
				
				$body .= $tmp_content;				
				switch ($arr_value["type"]) {
					case "text":
						$field = tep_draw_input_field($key, "", $arr_value["parameters"], false, $arr_value["type"], true);
						break;
					case "textarea":
						$field = tep_draw_textarea_field($key, "", "", 5, "", $arr_value["parameters"]) ;					
						break;
					case "pull_down":
						$arr_list = get_array_fulldown($arr_value["sql"], $arr_value["require"]);
						$field = tep_draw_pull_down_menu($key, $arr_list, $arr_value["default"], $arr_value["parameters"], false);
						break;
					case "image":
						$field = tep_draw_file_field($key,false,$arr_value["parameters"]);
						//print_r ($GLOBALS);
						//echo ("$key-".$$key."<br>");						
						if(isset($GLOBALS[$key])){				
							//echo ("$key-".$$key."<br>");
							$hinh = tep_output_string(stripslashes($GLOBALS[$key]));
							if(trim($hinh) != "" && strlen($hinh) > 0){
								//echo "hinh : $hinh<br>";
								//echo ("len:".strlen($hinh)."<br>" );
								$img_src = $arr["upload_path"].$hinh;
							}else{
								$img_src = "../images/blank_pic.gif";
							}
						}else{
							$img_src = "../images/blank_pic.gif";
						}
						$field_img = '
									<tr bgcolor="EFEFF5"> 
										<td align="right" class="record">&nbsp;</td>
										<td class="tbox">
											<table width="20%" border="3" bordercolor="#999999">
											<tr> 
											  <td><img src="'.$img_src.'" name=img_src id="img_'.$key.'"  ></td>
											</tr>
											</table>
										</td>
									</tr>						
						';
						break;
				}//end switch				 
				
				$body = ereg_replace("@text@",$arr_value["text"],$body);
				$body = ereg_replace("@field@",$field,$body);
				if ($field_img != ""){
					$body .= $field_img;
					$field_img = "";
				}
				
			}//end for
		}
		$html = ereg_replace("<!--list_content-->", $body, $html);
		$html = ereg_replace("@which_page@", "$which_page", $html);

		return $html;
	}//end new()
	
	function _submit($table, $fields_arr){
		return insert ("$table",$fields_arr);
	}//end submit()
	
	function _edit($arr, $arr_column){
		return $this->_new($arr, $arr_column);

	}//end edit
	
	function _update($table, $fd_name, $id, $fields_arr){
		update ("$table","$fd_name",$id, $fields_arr);
	}//end update
	
	function _delete($id, $act){
		global $dbconn, $which_page, $cat_parent_filter, $cat_parent_filter_2;
		//echo "id $id - act $act<br>";
		if (isset($id))
		{
			switch ($act)
			{
				case "delete":
						$query ="delete from import where id in ($id)";				
						$dbconn->execute ($query);
						//==								
					break;
			}
		}
		
		$dbconn->stmtclose();
		$dbconn->close();
		
		//Thong bao ket qua
		$file_return = "import.php?which_page=$which_page&cat_parent_filter=$cat_parent_filter&cat_parent_filter_2=$cat_parent_filter_2";
		$button_name_error = "Tr&#7903; v&#7873; tr&#432;&#7899;c";
		$button_name_susccess = "Danh s&aacute;ch";
		if($error){
			$thongbao = $error_msg;
			$nut = '<input type="button" name="Button" value="'.$button_name_error.'" onClick="javascript:history.go(-1);">';
		}else{
			$thongbao = "Xóa thành công.";
			$nut = '<input type="button" name="Button" value="'.$button_name_susccess.'" onClick="javascript:document.location=\''.$file_return.'\'">';
		}
		
		
		//Thay the noi dung
		$str_content = loadHtml("../htmls/update_notify.htm");
		
		$str_content = ereg_replace("<!--error-->",$error_title,$str_content);
		$str_content = ereg_replace("@thongbao@",$thongbao,$str_content);
		$str_content = ereg_replace("@nut@",$nut,$str_content);
		return $str_content;		
		
	}//end deleted()
	
	function lietke($city, $state)
	{
		global $dbconn, $page_block, $which_page, $catid, $subcatid, 
		$langid, $arr_page, $current_pages ;
		
		$page_size = PAGE_SIZE;
		
		$column = 1;
		$arrtmp = loadtmpfile("../htmls_templates/tmp_import_list.htm");
		$html = $arrtmp['layout'];
		$tmp_giatri = $arrtmp['body'];
		$list_begin = $arrtmp['begin'];
		$list_end = $arrtmp['end'];
		
		$str_sanpham = "";
		$col=0;
		$colspan = $column * 2 - 1;
			
		$line_ngang = "";
		$line_dung = "";
		
		$sql = "select * from import where city = '$city' and state = '$state' order by name";
		
		$rs_if= new db_query($dbconn,$sql);
		if ($rs_if->getrow()){
			$totalrows = mysql_affected_rows();
			//Get Paging
			if(!$which_page || !is_numeric($which_page)){
				$which_page = 1;
			}
			$num_page = (int)($totalrows/$page_size);
			if($totalrows%$page_size != 0){
				$num_page ++;
			}
			//$num_row = $page_size;
			if($which_page > $num_page) $which_page = $num_page;
			if($which_page == 1){
				$index = 0;
			}else{
				$index = ($which_page*$page_size) - $page_size;
			}
			
		
			$body = "";
			$var_dynimages_arr = "";
			$i=0;
			$stt = 1;
			
			if($totalrows < $page_size){
				$sohang = $totalrows;
			}else{
				$sohang = $page_size;				
			}
			$line_num = (int)($sohang/$column);
			if($sohang%$column != 0){
				$line_num ++;
			}			
			
			//echo "totalrows: $totalrows - page_size: $page_size - num_page: $num_page - which_page: $which_page - column: $column<br>";
			
			$rs = new db_query($dbconn, $sql);	
			while ($rs->getrow()){
				//echo "================================== i:$i - index: $index - page_size: $page_size<br>";
				if (($i >= $index) && ($i < $page_size + $index))
				{
					//echo "i:$i<br>";
					if($col==0){
						$str_sanpham .= "<tr>"; 
					}
					$fld_id = $rs->row["id"];
					$name = $rs->row["name"];
					$name_url = text_url($name); 
					$address = $rs->row["address"];
					$city = $rs->row["city"];
					$state = $rs->row["state"];
					$zipcode = $rs->row["zipcode"];
					$tel1 = $rs->row["tel1"];
					$tel2 = $rs->row["tel2"];
					$tel3 = $rs->row["tel3"];
					$tel = "($tel1) $tel2-$tel3";
					
					$str_sanpham .= "<td align='left' valign='top' >".$tmp_giatri."</td>";
					//Line dung
					if($col < $column-1){
						$str_sanpham .= "
						
						";
					}
					
					$str_sanpham=ereg_replace("@id@", "$fld_id", $str_sanpham);
					$str_sanpham=ereg_replace("@name@", "$name", $str_sanpham);
					$str_sanpham=ereg_replace("@name_url@", "$name_url", $str_sanpham);
					$str_sanpham=ereg_replace("@address@", "$address", $str_sanpham);
					$str_sanpham=ereg_replace("@tel@", "$tel", $str_sanpham);
					$str_sanpham=ereg_replace("@city@", "$city", $str_sanpham);
					$str_sanpham=ereg_replace("@state@", "$state", $str_sanpham);
					$str_sanpham=ereg_replace("@zipcode@", "$zipcode", $str_sanpham);
															
					$col++;
					//echo "stt:$stt<br>";
					if($col==$column)
					{
						$str_sanpham.="</tr>";

						if($stt < $line_num){
							$str_sanpham.="<tr>";
							
							for($i_column = 1; $i_column < $column; $i_column ++){
								$str_sanpham.="
								";
							}
							$str_sanpham.="
							";
						}	
						
						$col=0;
						$stt++;
						

					}
				}//end if index
				$i++;
				
				
				
			}//end while
			
			$body = $list_begin.$str_sanpham.$list_end;
			
			
			//Lat trang
			
			$StringQuery = "&proid=$proid&type=$type&catid=$catid&subcatid=$subcatid&langid=$langid\"";
			$str_paging = "";
			if($num_page>1){
				if($which_page > 1 ){
					$str_paging = "<a href=\"?which_page=1$StringQuery><img src=../css/images/first.gif width=15 height=13 align=absmiddle border=0></a>";
					$str_paging .= "&nbsp;<a href=\"?which_page=".($which_page-1)."$StringQuery ><img src=../css/images/prev.gif width=7 height=13 align=absmiddle border=0></a>&nbsp;";
				}else{
					$str_paging = "";
				}
			
			//=======================================================
				$str_paging .= list_page_css($which_page,$num_page,$page_block,$StringQuery);
			//========================================================
			
				if($which_page < $num_page ){
					$str_paging .= "&nbsp;<a href=\"?which_page=".($which_page+1)."$StringQuery  ><img src=../css/images/next.gif width=7 height=13 align=absmiddle border=0></a>";
					$str_paging .= "<a href=\"?which_page=".($num_page)."$StringQuery><img src=../css/images/last.gif width=15 height=13 align=absmiddle border=0> </a>";
				}else{
					$str_paging .= "";
				}
			}//end if $num_page > 1			
	
			
		}//end if rs_if
		
		//===============
		//echo "num_page: $num_page<br>";
		if ($num_page > 0){
			$record_num .= $arr_page[$langid]." $which_page/$num_page ";
		}else{
			$record_num .= "";
		}
		
		//
		if ($subcatid > 0){
			$img_id = $subcatid;
		}else{
			$img_id = $catid;
		}
	
		
		$html = ereg_replace("@list_content@", $body, $html);
		$html = ereg_replace("@PAGE@", "$str_paging", $html);
		$html = ereg_replace("@TOTAL@", "$record_num", $html);
		$html = ereg_replace("@which_page@", "$which_page", $html);				
		$StringQuery = ereg_replace("\"","",$StringQuery);
		$html = ereg_replace("@StringQuery@", "$StringQuery", $html);		
		$html = ereg_replace("@langid@", "$langid", $html);
		
		return $html;
	}//end lietke		
	
	function import_baonail(){
		global $dbconn;
		
		$cities = new class_cities();
		
		$arr_cities = $cities->import_by_state();
		
		$table = "cities";
		$fd_name = "id";
		$today = today_fulltimestamp();
		$content = "";
		$total = 0;
		if(count($arr_cities) > 0){
			foreach ($arr_cities as $key=>$arr){
				$city = ereg_replace(" ", "%20", $arr['city']);
				$state = $arr['state'];
				$url = "http://baonail.com/?state=".$state."&city=".$city."&page=";
				$id = $key;
				for($i=1;$i<=4;$i++){
					$url_ = $url."$i";
					//echo "$url_<br>";
					$content .= $this->parser_baonail($url_);
				}
				$import_url = $url."1";
				//$content = safe_sql($content);
				
				$arr_nail = $this->split_array_baonail($content);
				//print_r ($arr_nail);echo "<br>";
				$total_add = $this->insert_by_array($arr_nail);
				$total += $total_add;
				$fields_arr = array(			
						"import" => "''",
						"import_url" => "'$import_url'",
						"import_status"=>"'1'",
						"import_date"=>"'$today'",
						"import_total"=>"'$total_add'"
						);
				//echo "$table - $fd_name = $id";
				//print_r ($fields_arr);
				$cities->_update($table, $fd_name, $id, $fields_arr);
			}
		}//end if
		return $total;
	}
	
	function import_yp(){
		global $dbconn;
		
		$cities = new class_cities();
		
		$arr_cities = $cities->import_by_state();
		
		$table = "cities";
		$fd_name = "id";
		$today = today_fulltimestamp();
		$content = "";
		$total = 0;
		if(count($arr_cities) > 0){
			foreach ($arr_cities as $key=>$arr){
				//$city = ereg_replace(" ", "%20", $arr['city']);
				$city = ereg_replace(" ", "+", $arr['city']);
				$state = $arr['state'];
				$city_state = strtolower($city)."-".strtolower($state);
				//http://www.yellowpages.com/copper-center-ak/nail?g=Copper+Center%2C+AK
				//$url = "http://baonail.com/?state=".$state."&city=".$city."&page=";
				//$url = "http://www.yellowpages.com/anchorage-ak/nail?g=Anchorage%2C+AK&page=1&refinements[headingtext]=Nail+Salons";
				$url = "http://www.yellowpages.com/".$city_state."/nail?g=".$city."%2C+".$state."&refinements[headingtext]=Nail+Salons&page=";
				$id = $key;
				$p_from = 3;
				$p_to = 4;
				for($i=$p_from;$i<=$p_to;$i++){
					$url_ = $url."$i";
					//echo "$url_<br>";
					$arr_nail = $this->parser_yp($url_);
					$total_add = $this->insert_by_array($arr_nail);
					$total += $total_add;
				}
				$import_url = $url.$p_from;
				//$content = safe_sql($content);
				
				$fields_arr = array(			
						"import" => "''",
						"import_url" => "'$import_url'",
						"import_status"=>"'1'",
						"import_date"=>"'$today'",
						"import_total"=>"'$total_add'"
						);
				//echo "$table - $fd_name = $id";
				//print_r ($fields_arr);
				$cities->_update($table, $fd_name, $id, $fields_arr);
			}
		}//end if
		return $total;
	}//import_yp
	
	function import_video_baonail()
	{
		$url = "http://baonail.com/video.php?&page=";
		$total = 0;
		for($i=1;$i<=4;$i++){
			$url_ = $url."$i";
			//echo "$url_<br>";
			$arr_video = $this->parser_baonail_video($url_);
			//print_r ($arr_video);echo"<br>";
			$total_add = $this->insert_by_array_video($arr_video);
			$total += $total_add;
		}
		return $total;
	}//end import_video_baonail
	
	function parser_baonail($url)
	{
		// get DOM from URL or file
		$html = file_get_html($url);
		
		//============ khu noi dung
		// remove all image
		foreach($html->find('img') as $e)
			$e->outertext = '';
		
		// Find all element which class=foo
		foreach($html->find('.ads1') as $e)
			$e->outertext = '';
		
		// Find all tag select
		foreach($html->find('select') as $e)
			$e->outertext = '';
		
		// Find all tag font
		foreach($html->find('font') as $e)
			$e->outertext = '';
			
		// find all div 
		foreach($html->find("div[style='border: 1px solid #E4E4E4;padding:5px;']") as $e){
			$content .= $e->innertext;
			//echo $e->innertext . '<br>';
		}
		
		$html_2 = str_get_html($content);
		$result =  $html_2->plaintext;
		$result = trim($result);
		$result = ereg_replace("&nbsp;&nbsp;\[  \]", "", $result);
		$result = ereg_replace("Tiệm trong khu vực", "", $result);
		$result = ereg_replace("\t", "", $result);
		$result = ereg_replace("&nbsp;&nbsp;", "", $result);
		
		return $result;
		
	}//end parser_baonail
	
	function parser_yp($url)
	{
		$html = file_get_html($url);
		$arr = array();
		$i=0;
		foreach($html->find('div.listing_content') as $e){
			//$arr[] = $e->outertext;
			$noidung = $e->outertext;
			$html_2 = str_get_html($noidung);
			
			
			// find all span tags with class=gb1
			//echo "<br>===================<br>";
			//*
			foreach($html_2->find('a.url') as $ret)
				$name =  trim_full($ret->plaintext);
			foreach($html_2->find('span.street-address') as $ret)
				$address =  trim_full($ret->plaintext);
			foreach($html_2->find('span.business-phone') as $ret)
				$phone =  trim_full($ret->plaintext);		
			foreach($html_2->find('span.locality') as $ret)
				$city =  trim_full($ret->plaintext);
			foreach($html_2->find('span.region') as $ret)
				$region =  trim_full($ret->plaintext);
			foreach($html_2->find('span.postal-code') as $ret)
				$zipcode =  trim_full($ret->plaintext);
			//*/
			$address = ereg_replace(",","",$address);
			$phone = ereg_replace("[\(\)]","",$phone);
			//echo "name: $name<br>";
			//echo "address: $address<br>";
			//echo "city: $city<br>";
			//echo "region: $region<br>";
			//echo "zipcode: $zipcode<br>";
			//echo "phone: $phone<br>";
			
			$tel = ereg_replace("[\(\)]", "", $phone);
			$tel_arr = preg_split ("/[\s?-]+/", $phone);
			
			$arr[$i]['name'] = $name;
			$arr[$i]['address'] = $address;
			$arr[$i]['city'] = $city;
			$arr[$i]['state'] = $region;
			$arr[$i]['zipcode'] = $zipcode;
			$arr[$i]['phone'] = $tel_arr;
			
			//print_r ($tel_arr);
			$i++;
		}//end for
		
		return $arr;
	}//end parser_yp	
	
	function parser_baonail_video($url)
	{
		$html = file_get_html($url);
		$arr = array();
		$i=0;
		// find all div tags with class=video_item
		foreach($html->find('div.video_item') as $e){
			//$arr[] = $e->outertext;
			$noidung = $e->outertext;
			$html_2 = str_get_html($noidung);
			
			foreach($html_2->find('img') as $ret)
				$hinhanh =  trim($ret->src);
			foreach($html_2->find('span') as $ret)
				$title =  trim($ret->plaintext);
			
			$arr_url = explode("/", $hinhanh);
			$id = $arr_url[4];
			
			$arr[$i]['code'] = $id;
			$arr[$i]['title'] = $title;
			
			$i++;
		}//end for
		
		return $arr;
	}//end parser_yp
	
	function split_array_baonail($content)
	{
		//echo "$content<br>";
/*		
		$content = "
&nbsp;&nbsp;B Bella Hair Design
&nbsp;&nbsp;642 S Alaska St
&nbsp;&nbsp;Palmer, AK, 99645

&nbsp;&nbsp;Country Cutts
&nbsp;&nbsp;432 S Alaska St
&nbsp;&nbsp;Palmer, AK, 99645

&nbsp;&nbsp;Noels Hair Salon
&nbsp;&nbsp;550 S Alaska St Ste 2
&nbsp;&nbsp;Palmer, AK, 99645
		";
*/		
		$arr = explode( "\r", $content );
		//print_r ($arr);echo"br>";
		$i=1;
		$idx=0;
		foreach($arr as $value){
			$value = trim($value);
			$value = ereg_replace("\r", "", $value);
			$value = ereg_replace("\n", "", $value);
			if($value!=""){
				switch($i){
					case 1:
						$arr_nails[$idx]['name']=$value;
						$i++;					
						break;
					case 2:
						$arr_nails[$idx]['address']=$value;
						$i++;
						break;
					case 3:
						list ($city, $state, $zipcode) = split ('[,]', $value);
						$arr_nails[$idx]['city'] = trim($city);
						$arr_nails[$idx]['state'] = trim($state);
						$arr_nails[$idx]['zipcode'] = trim($zipcode);
						break;
				}//end switch

			}else{
				$i=1;
				$idx++;
			}//end if
		}//end foreach
		//print_r ($arr_nails);
		/*
		$patent = "/&nbsp;&nbsp;[a-zA-Z0-9 ,]+
		&nbsp;&nbsp;[a-zA-Z0-9 ,]+
		&nbsp;&nbsp;[a-zA-Z0-9 ,]+/";
		preg_match_all ($patent, $content, $out, PREG_PATTERN_ORDER);
		print_r($out);echo"<br>";
		$arr_nails = array();
		$patent_1 = "/&nbsp;&nbsp;[a-zA-Z0-9 ,]+/";
		$i=0;
		foreach ($out[0] as $key=>$value){
			preg_match_all($patent_1, $value, $out_2, PREG_PATTERN_ORDER);
			//print_r ($out_2[0]);
			$arr_nails[$i]['name'] = ereg_replace("&nbsp;&nbsp;", "", $out_2[0][0]);
			$arr_nails[$i]['address'] = ereg_replace("&nbsp;&nbsp;", "", $out_2[0][1]);
			$city_state = ereg_replace("&nbsp;&nbsp;", "", $out_2[0][2]);
			list ($arr_nails[$i]['city'], $arr_nails[$i]['state'], $arr_nails[$i]['zipcode']) = split ('[,]', $city_state);
			$i++;
		}
		*/
		
		return $arr_nails;	
	}//end split_array_baonail
	
	function insert_by_array($arr){
		$nails = new class_nails();
		$cities = new class_cities();
		$table = "nails";
		$cnt = 0;
		//print_r ($arr);
		if(count($arr) > 0){
			foreach ($arr as $value){
				$name = $value['name'];
				$address = $value['address'];
				$city = $value['city'];
				$state = $value['state'];
				$zipcode = $value['zipcode'];
				$phone = $value['phone'];
				$tel1 = $phone[0];
				$tel2 = $phone[1];
				$tel3 = $phone[2];
				$fields_arr = array(			
						"name" => "'$name'",
						"tel1" => "'$tel1'",
						"tel2" => "'$tel2'",
						"tel3" => "'$tel3'",
						"address" => "'$address'",
						"city"=>"'$city'",
						"state"=>"'$state'",
						"zipcode"=>"'$zipcode'"
						);
				
				$sql = "select id from $table where name = '$name' and tel1='$tel1' and tel2='$tel2' and tel3 = '$tel3' ";
				//echo "$sql <br>";
				if(!(TEST_EXIST_ITEM($sql))){
					if($name != "" && $tel1 != "" && $city != "" && $state != "" && $zipcode != "" ){
						//*
						$sql_city = "select * from cities where name='$city' and state='$state'";
						if(!(TEST_EXIST_ITEM($sql_city))){
							$fields_arr_city = array(
								"name" => "'$city'",
								"state" => "'$state'"
								);
							$cities->_submit("cities", $fields_arr_city);
						}
						//*/
						$nails->_submit($table, $fields_arr);
						$cnt++;
					}
				}
			}
		}
		return $cnt;
	}//insert_by_array
	
	function insert_by_array_video($arr){
		$video = new class_video();
		$table = "video";
		$cnt = 0;
		//print_r ($arr);
		if(count($arr) > 0){
			foreach ($arr as $value){
				$title = $value['title'];
				$code = $value['code'];
				$fields_arr = array(			
						"title" => "'$title'",
						"code" => "'$code'",
						);
				
				$sql = "select id from $table where code = '$code' ";
				//echo "$sql <br>";
				if(!(TEST_EXIST_ITEM($sql))){
					if($title != "" && $code != "" ){
						$video->_submit($table, $fields_arr);
						$cnt++;
					}
				}//end if
			}
		}
		return $cnt;
	}//insert_by_array_video	
	
	function import_tel_whitepages()
	{
		global $dbconn;
		$sql = "select * from nails where update_tel='0' and name <> '' and zipcode <> '' and tel1='' and tel2='' and tel3='' order by state, city, name limit 0,1";
		$rs = new db_query($dbconn, $sql);
		if($rs->getrow()){
			$id = $rs->row['id'];
			$address = $rs->row['address'];
			$zipcode = $rs->row['zipcode'];
			
			$address = ereg_replace(" ","+", $address);
			$url = "http://www.whitepages.com/search/ReverseAddress?street=".$address."&where=".$zipcode;
			
			echo "address: $address - zipcode: $zipcode<br>";
			echo "<a href=$url target=_blank>$url</a><br>";
			
			$sql_ = "update nails set update_tel = '1' where id='$id'";
			$dbconn->execute($sql_);
		}
	}//import_tel_whitepages
	
	function import_tintuc_vietbao($tintuc_catid, $url)		
	{
		global $dbconn;
		$doc = new DOMDocument();
		$doc->load($url);
		$arrFeeds = array();

		foreach ($doc->getElementsByTagName('item') as $node) {
			$ngaythang = strtotime($node->getElementsByTagName('pubDate')->item(0)->nodeValue);
			$itemRSS = array ( 
				'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
				'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
				'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
				'date' => $ngaythang
				);
			array_push($arrFeeds, $itemRSS);
		}
		
		foreach ($arrFeeds as $key=>$arr){		
				$title = UnicodeEntity($arr['title']);
				$link = $arr['link'];
				$motangan = UnicodeEntity($arr['desc']);
				$ngaythang = $arr['date'];		
				$chitiet = "";		
		
				$sql = "select * from tintuc where title = '$title'";
				if(!TEST_EXIST_ITEM($sql)){
					//noi dung chi tiet
					$chitiet = $this->get_chitiet_vietbao($link);
					//insert vao database
					$fields_arr = array(			
							"title" => "'$title'",
							"chitiet" => "'$chitiet'",
							"ngaythang" => "'$ngaythang'",
							"tintuc_cat" => "'$tintuc_catid'"
							);
					$id = insert ("tintuc",$fields_arr);
					$link_new = "tintuc_chitiet.php?catid=$tintuc_catid&subcatid=0&newsid=$id&langid=0";
					$motangan = ereg_replace($link, $link_new, $motangan);
					$sql = "update tintuc set motangan='$motangan' where id = '$id'";
					$dbconn->execute($sql);
					$cnt_tintuc ++;
					//echo "$cnt_tintuc - ";		
				}else{
					//echo "--";
				}
				//echo "$title - <br>\n";
				
		}//end for
		return $cnt_tintuc;
		
	}//import_tintuc_vietbao

	function get_chitiet_vietbao($url)
	{
		$str_begin = "<!-- Article Start -->";
		$str_end = "<!-- Article End -->";
		//$html = get_content_url($url, $str_begin, $str_end);
		//$html = str_get_html($html);
		$html = file_get_html($url);
		$html = $html->save();
		$html = get_sub_string_2($html,$str_begin,$str_end);
		$html = str_get_html($html);
		foreach($html->find('.tags1') as $e)
			$e->outertext = '';
		foreach($html->find('.tags') as $e)
			$e->outertext = '';	
		foreach($html->find('script') as $e)
			$e->outertext = '';				
		
		$chitiet = $html->save();
		$chitiet = UnicodeEntity($chitiet);	
		$chitiet = str_replace("'",'"',$chitiet);
		//echo "$chitiet<br><br>=============================<br>\n";
		return $chitiet;
	}//get_chitiet_vietbao
	
	function import_tintuc_reset($second)
	{
		global $dbconn;
		$today = today_fulltimestamp();
		$chenhlech = $today - $second;
		$sql = "update contents set import_status = '0' where import_date <= $chenhlech ";
		//echo "$sql<br>";
		$dbconn->execute($sql);
	}

}//end class_import
?>